package com.example.online_exam_system.controller;

import com.example.online_exam_system.commons.Result;
import com.example.online_exam_system.commons.TableResult;
import com.example.online_exam_system.logic.IStudentLogicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author lianglong
 * @create 2021-12-01-13:20
 * @describe 学生业务控制层
 */
@RestController
@Slf4j
public class StudentController {
    @Autowired
    HttpSession session;
    @Autowired
    IStudentLogicService studentLogicService;


    /**
     * @description 获取学生考试试卷列表
     * @param
     * @return
     * @author lianglong
     */
    @RequestMapping("getstuexam")
    public TableResult getstuexam(){
        return studentLogicService.getstuexam();
    }

    /**
     * 获取答题信息，入表
     * @param map
     * @return
     */
    @PostMapping("submmitExam")
    public String submmitExam(@RequestParam Map<String,Object> map){
        Result result = studentLogicService.submmitExam(map);
        if(result.getCode() == 0){
            return "examsucess";
        }else{
            return "examfail";
        }
    }

    /**
     * 学生成绩查询
     * @return
     */
    @PostMapping("queryScore")
    public TableResult queryScore(){
        String username = (String)session.getAttribute("username");
        return studentLogicService.queryScore1(username);
    }

    /**
     * 学生信息更新
     * @param inParam
     * @return
     */
    @PostMapping("updateStudentInfo")
    public Result updateStudentInfo(@RequestBody Map<String,Object> inParam){
        //更新基本信息
        int i = studentLogicService.updateStudentInfo(inParam);
        if(i == 3){
            return new Result().success(true).msg("更新成功").data("");
        }else{
            return new Result().success(false).msg("更新失败").data("");
        }
    }

    /**
     * @description 检验教师密码是否正确
     * @param
     * @return
     * @author lianglong
     */
    @RequestMapping(value = "/pwdVerify2",method = RequestMethod.POST)
    public Result pwdVerify2(@RequestBody Map<String,Object> inParam){
        int i =studentLogicService.pwdVerify2(inParam);
        if(i>0){
            return new Result().success(true).msg("校验成功");
        }else{
            return new Result().success(false).msg("校验失败");
        }

    }

    @GetMapping("queryStuAll")
    public TableResult queryStuAll(){
        List<Map<String, Object>> stuAll = studentLogicService.getStuAll();
        return TableResult.ok().data(stuAll).msg("获取成功").count(String.valueOf(stuAll.size()));
    }


}
